<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  Form
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: Validation.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * Form validation class.
 *
 * @todo Validation is not form specific, could be used for everything!
 *
 * @package     mpContenidoX
 * @subpackage  Form
 * @author      Murat Purc <murat@purc.de>
 */
class mpForm_Validation
{
/*
    public function setForm(mpForm $form)
    {
        $this->_form = $form;
    }
    public function setValidations($validations)
    {
        $this->_form = $form;
    }
    public function validate()
    {
        foreach ($form->getRequest() as $name => $value) {
        }
    }
*/

    public static function regex($value, $regex)
    {
        return preg_match($regex, $value);
    }


    public static function equals($value, $otherValue)
    {
        return ($value === $otherValue);
    }


    public static function minValue($value, $minValue)
    {
        return ($value >= $minValue);
    }


    public static function maxValue($value, $maxValue)
    {
        return ($value <= $maxValue);
    }


    public static function rangeValue($value, $minValue, $maxValue)
    {
        return ($value >= $minValue && $value < $maxValue + 1);
    }


    public static function minLength($value, $minLength)
    {
        return (strlen($value) >= $minValue);
    }


    public static function maxLength($value, $maxLength)
    {
        return (strlen($value) <= $maxLength);
    }


    public static function rangeLength($value, $minLength, $minLength)
    {
        return (strlen($value) >= $minLength && strlen($value) < $minLength + 1);
    }


    public static function maxFileSize(array $uploadedFile, $maxAllowedSize)
    {
        if (isset($uploadedFile['size'])) {
            return false;
        }

        $size = (int) $uploadedFile['size'];
        if ($size == 0) {
            return true;
        }

        return (($size / (1024*1024)) > (int) $maxAllowedSize);
    }


    public static function allowedMimeType(array $uploadedFile, $allowedMimeTypes)
    {
        if (isset($uploadedFile['type'])) {
            return false;
        }

        $allowedMimeTypes = explode(',', $allowedMimeTypes);
        foreach ($allowedMimeTypes as $p => $v) {
            $allowedMimeTypes[$p] = trim($v);
        }

        return (in_array($uploadedFile['type'], $allowedMimeTypes));
    }

}